package com.rec.smile.web;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.rec.smile.bo.SecKill;
import com.rec.smile.bo.SuccKillUser;
import com.rec.smile.dto.Exposer;
import com.rec.smile.exception.BusinessException;
import com.rec.smile.exception.ErrorCode;
import com.rec.smile.service.SecKillService;
import com.rec.smile.util.TimeUtil;

@Controller
@RequestMapping("/secKill")
public class SeckillController {

	private static final Logger LOGGER =LoggerFactory.getLogger(SeckillController.class);
	
	@Autowired
	private SecKillService secKillService;
	
	
	@RequestMapping(value="/list", method=RequestMethod.GET)
	public String list(Model model){
		model.addAttribute("list", secKillService.getSecKills());
		return "list";
	}
	
	@RequestMapping(value="/{secKillId}/detail", method=RequestMethod.GET)
	public String detail(@PathVariable("secKillId") Long secKillId, Model model){
		if(secKillId == null){
			return "redirect:/secKill/list";
		}
		SecKill secKill = secKillService.getSecKill(secKillId);
		if(secKill == null){
			return "forward:/secKill/list";
		}
		model.addAttribute("secKill", secKill);
		return "detail";
	}
	
	@RequestMapping(value="/{secKillId}/exposer", produces={"application/json;charset=utf-8"}, method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> exposer(@PathVariable("secKillId")Long secKillId){
		try {
			Exposer secKillUrl = secKillService.export2SecKillUrl(secKillId);
			Map<String,Object> ret = new HashMap<>();
			ret.put("data", secKillUrl);
			ret.put("status", ErrorCode.success);
			return ret;
		} catch (Exception e) {
			return handerException(e);
		}
		
	}
	
	@RequestMapping(value="/{secKillId}/{md5}/execSecKill", produces={"application/json;charset=utf-8"}, method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> execSecKill(@PathVariable("secKillId")Long secKillId, @PathVariable("md5") String md5
			,@CookieValue(value="phoneNum",required=false) String phone){
		try {
			SuccKillUser secKilluser = secKillService.execSecKill(secKillId, phone, md5);
			Map<String,Object> ret = new HashMap<>();
			ret.put("data", secKilluser);
			ret.put("status", ErrorCode.success);
			return ret;
		} catch (Exception e) {
			return handerException(e);
		}
		
	}
	
	@RequestMapping(value="/time/now", produces={"application/json;charset=utf-8"}, method=RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> now(){
		Map<String,Object> ret = new HashMap<>();
		ret.put("data", TimeUtil.currentTimeMillis());
		ret.put("status", ErrorCode.success);
		return ret;
	}
	
	private Map<String, Object> handerException(Throwable t){
		LOGGER.error("", t);
		Map<String, Object> ret = new HashMap<String, Object>();
		ret.put("status", ErrorCode.system_err);
		if(t instanceof BusinessException){
			BusinessException businessException = (BusinessException) t;
			ret.put("errorCode", businessException.getCode());
			ret.put("errMsg", businessException.getErrMsg());
		}else{
			ret.put("errorCode", ErrorCode.system_err);
		}
		return ret;
	}
}
